
import 'package:boss/main.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/offers.dart';
import 'package:boss/models/sku.dart';
import 'package:boss/ui/page/offersManagement/constant/offers_constant.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/provider/base_provider.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:hi_common/hi_common.dart';

class BuyGiftOffersProvider extends BaseProvider {
  BuyGiftOffersProvider({Offers data}) {
    super.initData(data: data);
    if (data != null) {
      buyCount = data.buyGiveDiscountlist[0].buyCount.toString();
      giftCount = data.buyGiveDiscountlist[0].giveCount.toString();

      data.buyGiveDiscountlist.forEach((item) {
        var commodity = Commodity.fromJson(
            {'title': item.buyComPropertiesDTO.title, 'id': item.commodityId, 'price': item.buyComPropertiesDTO.price, 'unit': item.buyComPropertiesDTO.unit});

        if (item.skuId != 0) {
          commodity.commoditySkuList = [
            Sku.fromJson({
              'skuId': item.skuId,
              'skuName': item.buyComPropertiesDTO.commoditySkuName,
              'skuPrice': item.buyComPropertiesDTO.price,
            })
          ];
        }

        commoditys.add(commodity);

        var giftCommodity = Commodity.fromJson({
          'title': item.giveComPropertiesDTO.title,
          'id': item.giveCommodityId,
          'price': item.giveComPropertiesDTO.price,
          'unit': item.giveComPropertiesDTO.unit,
        });
        if (item.giveSkuId != 0) {
          giftCommodity.commoditySkuList = [
            Sku.fromJson({
              'skuId': item.giveSkuId,
              'skuName': item.giveComPropertiesDTO.commoditySkuName,
              'skuPrice': item.giveComPropertiesDTO.price,
            })
          ];
        }

        giftCommoditys.add(giftCommodity);
      });
    }
  }

  String buyCount;
  String giftCount;
  List<Commodity> giftCommoditys = [];

  setBuyCount(String value) {
    buyCount = value;
    notifyListeners();
  }

  setGiftCount(String value) {
    giftCount = value;
    notifyListeners();
  }

  setGiftCommoditys(List<Commodity> value) {
    giftCommoditys = value;
    notifyListeners();
  }

  removeGiftCommodity(Commodity value) {
    giftCommoditys.remove(value);
    notifyListeners();
  }

  Future<bool> addBuyGiftOffers() async {
    try {
      if (offerName == null || offerName == '') {
        toast('请填写优惠名称');
        return false;
      }
      if (buyCount == null || buyCount == '') {
        toast('请输入购买份数');
        return false;
      }
      if (giftCount == null || giftCount == '') {
        toast('请输入赠送份数');
        return false;
      }
      if (commoditys.length == 0) {
        toast('请选择参与活动的菜品');
        return false;
      }
      if (giftCommoditys.length == 0) {
        toast('请选择赠送的菜品');
        return false;
      }

      showLoadingToast();

      Map<String, dynamic> data = {
        'buyCount': int.parse(buyCount),
        'giveCount': int.parse(giftCount),
        'commodityCustomList': commoditys
            .map((item) => {
                  'commodityId': item.id,
                  'skuId': item.sku?.skuId,
                })
            .toList(),
        'giveCommodityCustomList': giftCommoditys
            .map((item) => {
                  'commodityId': item.id,
                  'skuId': item.sku?.skuId,
                })
            .toList(),
        'storeDiscountStandard': {
          'activityName': offerName,
          'activityType': activityType,
          'discountType': OffersConstant.buy_gift_discount,
          'type': OffersConstant.commodity_offers,
          'supportQrcode': 0,
        },
      };

      setDateTimeForHttpData(data);

      var result = await api.addBuyGiveDiscount(data);

      if (!result.successful()) {
        toast(result.msg);
        return false;
      }
      toast('添加成功');
      return true;
    } catch (e) {
      toast(e.toString());
      return false;
    } finally {
      hideLoadingToast();
    }
  }
}
